//! # Pattern
//!
//! 提供标准的游戏身份策略
use sgs_core::player::Identity;

use crate::pkg::Pattern;

/// 身份策略枚举类型
///
/// 生成符合标准的 `sgs_core::pkg::Pattern`
///
/// 从 2 人到 12 人
#[derive(Debug, Clone, PartialEq)]
pub enum PatternOption {
    /// 未知模式，表示错误类型
    Unknown,

    Player2,
    Player3,
    Player4,
    Player5,
    Player6,
    Player7,
    Player8,
    Player9,
    Player10,
    Player11,
    Player12,
}

impl PatternOption {
    /// 判断玩家数量是否符合要求
    ///
    /// 需要为 2..=12
    pub fn valid_players(players_count: usize) -> bool {
        return 0 < players_count && players_count < 13;
    }

    /// 判断是不是正确的 Pattern
    pub fn is_valid(&self) -> bool {
        match self {
            Self::Unknown => false,
            _ => true,
        }
    }

    pub fn from_range(range_list: std::ops::Range<usize>) -> Vec<Pattern> {
        range_list
            .into_iter()
            .map(|n| Self::from(n).into())
            .collect()
    }
}

impl From<usize> for PatternOption {
    fn from(value: usize) -> Self {
        match value {
            2 => Self::Player2,
            3 => Self::Player3,
            4 => Self::Player4,
            5 => Self::Player5,
            6 => Self::Player6,
            7 => Self::Player7,
            8 => Self::Player8,
            9 => Self::Player9,
            10 => Self::Player10,
            11 => Self::Player11,
            12 => Self::Player12,
            _ => Self::Unknown,
        }
    }
}

impl Into<Pattern> for PatternOption {
    fn into(self) -> Pattern {
        match self {
            PatternOption::Unknown => Pattern {
                name: "未知模式".to_string(),
                identity_list: vec![],
                ..Default::default()
            },

            PatternOption::Player2 => Pattern {
                name: "2人场".to_string(),
                identity_list: vec![Identity::Lord, Identity::Rebel],
                ..Default::default()
            },
            PatternOption::Player3 => Pattern {
                name: "3人场".to_string(),
                identity_list: vec![Identity::Lord, Identity::Rebel, Identity::Traitor],
                ..Default::default()
            },
            PatternOption::Player4 => Pattern {
                name: "4人场".to_string(),
                identity_list: vec![
                    Identity::Lord,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Traitor,
                ],
                ..Default::default()
            },
            PatternOption::Player5 => Pattern {
                name: "5人场".to_string(),
                identity_list: vec![
                    Identity::Lord,
                    Identity::Loyal,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Traitor,
                ],
                ..Default::default()
            },
            PatternOption::Player6 => Pattern {
                name: "6人场".to_string(),
                identity_list: vec![
                    Identity::Lord,
                    Identity::Loyal,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Traitor,
                    Identity::Traitor,
                ],
                ..Default::default()
            },
            PatternOption::Player7 => Pattern {
                name: "7人场".to_string(),
                identity_list: vec![
                    Identity::Lord,
                    Identity::Loyal,
                    Identity::Loyal,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Traitor,
                ],
                ..Default::default()
            },
            PatternOption::Player8 => Pattern {
                name: "8人场".to_string(),
                identity_list: vec![
                    Identity::Lord,
                    Identity::Loyal,
                    Identity::Loyal,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Traitor,
                ],
                ..Default::default()
            },
            PatternOption::Player9 => Pattern {
                name: "9人场".to_string(),
                identity_list: vec![
                    Identity::Lord,
                    Identity::Loyal,
                    Identity::Loyal,
                    Identity::Loyal,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Traitor,
                ],
                ..Default::default()
            },
            PatternOption::Player10 => Pattern {
                name: "10人场".to_string(),
                identity_list: vec![
                    Identity::Lord,
                    Identity::Loyal,
                    Identity::Loyal,
                    Identity::Loyal,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Traitor,
                    Identity::Traitor,
                ],
                ..Default::default()
            },
            PatternOption::Player11 => Pattern {
                name: "11人场".to_string(),
                identity_list: vec![
                    Identity::Lord,
                    Identity::Loyal,
                    Identity::Loyal,
                    Identity::Loyal,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Traitor,
                ],
                ..Default::default()
            },
            PatternOption::Player12 => Pattern {
                name: "12人场".to_string(),
                identity_list: vec![
                    Identity::Lord,
                    Identity::Loyal,
                    Identity::Loyal,
                    Identity::Loyal,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Rebel,
                    Identity::Traitor,
                    Identity::Traitor,
                ],
                ..Default::default()
            },
        }
    }
}
